import java.net.InetAddress;
import java.net.UnknownHostException;

/**
 * @author Jimmy Zhan WORKSTATION
 * @date 2023/3/30 11:33
 * 说明：
 */

public class InetAddressTest {
    public static void main(String[] args) {
        try {
            // 1. 实例化
            // getByName(String host)
            InetAddress inet1 = InetAddress.getByName("192.168.200.2");
            System.out.println(inet1);

            InetAddress inet2 = InetAddress.getByName("www.baidu.com");
            System.out.println(inet2);

            // getLocalHost()
            InetAddress inet3 = InetAddress.getLocalHost();
            System.out.println(inet3);

            // 环回地址
            InetAddress inetLocal = InetAddress.getByName("127.0.0.1");
            System.out.println(inetLocal);

            // 2. 两个常用的方法
            // getHostName(): 获取域名
            System.out.println(inet2.getHostName());
            // getHostAddress(): 获取ip地址
            System.out.println(inet1.getHostAddress());
        } catch (UnknownHostException e) {
            throw new RuntimeException(e);
        }
    }
}
